<?php

/**
 * class Users
 *
 * Description for class Users
 *
 * @author:
*/
class Model_DbTable_Users extends Zend_Db_Table {
	
	/**
	 * Table name
	 *
	 * @var mixed 
	 *
	 */	
	protected $_name = 'users';
	
	/**
	* Primary key
	*
	* @var mixed 
	*
	*/
	protected $_primary = 'id';
	
	/**
	 * This is method login
	 *
	 * @param mixed $email This is a description
	 * @param mixed $password This is a description
	 * @return mixed This is the return value description
	 *
	 */	
	public function login($email, $password) {
		// Receive Zend_Auth instance
		$auth = Zend_Auth::getInstance();
		
		// Create Adapter for Zend_Auth, indication where in the database 
		//it should search for login and password for comparison        
		$adapter = new Zend_Auth_Adapter_DbTable($this->getAdapter(), 
			'users', 'email', 'password', "SHA1(?)");
		
		$adapter->setIdentity($email)
			->setCredential($password);
		
		// Checks and saves the result of authentication
		$result = $auth->authenticate($adapter);
		
		if ($result->isValid()) {
			// Successfully
			
			//$store = new Zend_Session_Namespace('Zend_Auth');
			$store = $auth->getStorage();			
			// It's possible to save the session in additional fields
			$user = $adapter->getResultRowObject(array('id', 'username', 'email', 'full_name'));						
			$store->write($user);
			
			// Set the time of user logged in
			//$store->setExpirationSeconds(24 * 3600);
			
			// If "remember" was marked
			if (isset($_POST['rememberme'])) {
				Zend_Session::rememberMe();
			} else {
				Zend_Session::forgetMe();
			}
			
			return true;
		}
		
		return $result->getMessages();
	}	
	
	/**
	 * Get user's information by ID
	 *
	 * @param int $id User ID
	 * @return mixed This is the return value description
	 *
	 */
	public function getUser($id) {
		$id = (int)$id;
		$row = $this->fetchRow('id = ' . $id);
		if (!$row) {
			throw new Exception("Count not find row $id");
		}
		return $row->toArray();
	}
	
	/**
	 * Get user's information by email
	 *
	 * @param string $email User's email
	 * @return mixed This is the return value description
	 *
	 */
	public function getUserByEmail($email) {
		$row = $this->fetchRow($this->_db->quoteInto('email = ?', $email));
		if (!$row) {
			throw new Exception("Count not find row $email");
		}
		return $row->toArray();    
	}		
	
	/**
	 * This is method addUser
	 *
	 * @param mixed $username This is a description
	 * @param mixed $email This is a description
	 * @param mixed $password This is a description
	 * @param mixed $fullName This is a description
	 * @return mixed This is the return value description
	 *
	 */	
	public function addUser($username, $email, $password, $fullName) {
		$password = sha1($password);
		$data = array('username' => $username,
			'full_name' => $fullName,
			'email' => $email,
			'password' => $password,
			'created' => date("Y-m-d H:i:s")
			);
		$this->insert($data);		
	}
	
	/**
	 * 
	 *
	 * @param mixed $id This is a description
	 * @param mixed $username This is a description
	 * @param mixed $email This is a description
	 * @param mixed $password This is a description
	 * @param mixed $fullName This is a description
	 * @return mixed This is the return value description
	 *
	 */	
	public function updateUser($id, $username, $email, $password, $fullName) {
		$password = sha1($password);
		$data = array('username' => $username,
			'full_name' => $fullName,
			'email' => $email,
			'password' => $password
			);
		$this->update($data, 'id = '. (int)$id);
	}
	
	/**
	 * Delete user by ID
	 *
	 * @param int $id User ID
	 * @return mixed This is the return value description
	 *
	 */
	public function deleteUser($id) {
		$this->delete('id = ' . (int)$id);
	}
	
	/**
	 * Delete user by email
	 *
	 * @param string $email User's email
	 * @return mixed This is the return value description
	 *
	 */
	public function deleteUserByEmail($email) {
		$this->delete($this->_db->quoteInto('email = ?', $email));
	}	
}
?>